<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller;

use app\common\controller\Admin;
use app\basic\controller\api\Security as SecurityApi;
use app\admin\controller\ajax\Library as LibraryAjax;
/**
 * 保障机构管理
 * @package app\Security
 */
class Security  extends Admin

{
    
    protected $api;
    protected $property;
    protected $category;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new SecurityApi();
		$this->property = new LibraryAjax();
		$this->category = 2;
	}
    /**
     * 首页
     * 
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$securityList = $this->api->securityList();
   	  	return $securityList;
   	  }
      return $this->fetch();
 	}

 	/**
 	 * 保障机构查询
 	 */
 	public function search() {
 		if(IS_AJAX){
			$securityList = $this->api->search(input('keyword'));
 			return $securityList;
 		}
 	}
    /**
     * 添加保障机构
     * 
     */
    public function add() {
   		 if(IS_POST){
   		 	$data = input('post.'); 
   		 	//有效性
            if(isset($data['isValid'])){
               $data['isValid'] = 1;
            }else{
               $data['isValid'] = 0;
             }
             return $this->api->add($data);
   		 }
   		 $typeList = $this->property->getResuceUnitType($this->category)->getData();
   		 $this->assign('typeList',$typeList);
         return $this->fetch();    
     }
 
    /**
     * 修改保障机构
     * 
     */
    public function edit($securityId = null) {
    	  if(IS_POST){
    	  	$data = input('post.');
                  //有效性
            if(isset($data['isValid'])){
               $data['isValid'] = 1;
            }else{
               $data['isValid'] = 0;
             }
    	  	return $this->api->edit($data,$securityId);
    	  }
    	  $typeList = $this->property->getResuceUnitType($this->category)->getData();
    	  $this->assign('typeList',$typeList);
     	  $security = model('Security')->find($securityId);
     	  $this->assign('info',$security);
          return $this->fetch();      
    }
    
    /**
     * 删除保障机构
     */
    public function del($securityId = null) {
    	if(IS_AJAX){
    		return $this->api->del($securityId);
    	}
    }


	/**
	 * 设置保障机构位置
	 * @param string $position
	 * @param string $enterprise
	 * @return mixed
	 */
	public function setBuilding($position = null,$securityId = null){
		$center = $this->api->getSecurityPosition($securityId)->getData();
		$center = $center['code']?$center['data']:null;
		$this->assign('center',$center);
		$this->assign('position',$position);
		return $this->fetch();
	}
    
    /**
     * 申请审核
     * @param string $id
     */
    public function audit($id = null){
    	return $this->api->audit($id);
    }


}